/*  Clase TableReader encargada de administrar
 * la lectura  de los archivos de excel.
 * 
 */
package DBMS;

import java.io.File;
import jxl.*;

/**
 *
 * @author DELL
 */
public class TableReader {

  protected Workbook set;
  protected Sheet page;
  protected int row;
  protected int maxRows;

  public TableReader(File f) throws Exception {
    set = Workbook.getWorkbook(f);
    page = set.getSheet(0);
    row = 0;
    maxRows = page.getRows();
  }

  public int getMaxRows() {
    return maxRows;
  }

  public int getRow() {
    return row;
  }

  public void setRow(int row) {
    if (row >= 0 && row < maxRows) {
      this.row = row;
    }
  }
  public boolean hasNext(){
    return row < maxRows;
  }
  public String[] read() throws Exception {
    if (row == maxRows) {
      return null;
    }
    Cell[] c = page.getRow(row);
    String[] s = new String[c.length];
    for (int i = 0; i < c.length; i++) {
      s[i] = c[i].getContents();
    }
    this.row++;
    return s;
  }

  public void finish() throws Exception {
    set.close();
  }

  protected void finalize() throws Throwable {
    try {
      set.close();
    } catch (Exception e) {
    } finally {
      super.finalize();
    }
  }
}